//
//  HeaderView.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/8/30.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "HeaderView.h"
#import "JHMacro.h"
#import "UIImageView+WebCache.h"
#import "ZWYNetTool.h"
@implementation HeaderView
// 头视图
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.array = [NSMutableArray array];
        [self NetworkRequests];
        [self setup];
    }
    return self;
}
// 网络请求
- (void)NetworkRequests {
    [ZWYNetTool GET:@"http://api.kuaikanmanhua.com/v1/topic_new/discovery_list?" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSLog(@"=====================成功啦");
        self.model = [dataModel modelWithDic:result[@"data"]];
        for (int i = 0; i < self.model.infos[0].banners.count; i ++) {
            NSLog(@"----------------------%ld",self.array.count);
        }
    } andFailureBlock:^(NSError *error) {
        NSLog(@"=====================出错啦");
    }];
}
// 自定义model的set方法
- (void)setModel:(dataModel *)model {
    _model = model;
    for (int i = 0; i < model.infos[0].banners.count; i ++) {
    [self.array addObject:model.infos[0].banners[i].pic];
    }
    [self.array insertObject:model.infos[0].banners.lastObject.pic atIndex:0];
    [self.array insertObject:model.infos[0].banners.firstObject.pic atIndex:self.array.count];
    for (int i = 0; i < self.array.count; i ++) {
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        [_imageView sd_setImageWithURL:self.array[i]];
        [self.scrollview addSubview:self.imageView];
    }
}
// 设置轮播图
- (void)setup {
    // scrollview
    self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    self.scrollview.delegate = self;
    // 偏移量
    _scrollview.contentOffset = CGPointMake(self.bounds.size.width, 0);
    // 滚动范围
    _scrollview.contentSize = CGSizeMake(self.bounds.size.width * 8, self.bounds.size.height);
    // 整页滚动
    _scrollview.pagingEnabled = YES;
    // 添加到self上
    [self addSubview:self.scrollview];
    // pageControl
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.bounds.size.width / 2 - 100, self.bounds.size.height - 50, 200, 50)];
    // 背景色透明
    self.page.backgroundColor = [UIColor clearColor];
    // 初始从第一页开始
    self.page.currentPage = 0;
    // 页数
    self.page.numberOfPages = 6;
    // 选中的圆点的颜色
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self.page addTarget:self action:@selector(pageAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.page];
    [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
- (void)timerAction {
    if (_scrollview.contentOffset.x / self.bounds.size.width == 6) {
        self.page.currentPage = 0;
        [_scrollview setContentOffset:CGPointMake(7 * self.bounds.size.width, 0) animated:YES];
    } else if (_scrollview.contentOffset.x / self.bounds.size.width == 7) {
        self.page.currentPage = 0;
        _scrollview.contentOffset = CGPointMake(self.bounds.size.width, 0);
        self.page.currentPage = 1;
        [_scrollview setContentOffset:CGPointMake(2 * self.bounds.size.width, 0) animated:YES];
    } else {
        self.page.currentPage = _scrollview.contentOffset.x / self.bounds.size.width;
        [_scrollview setContentOffset:CGPointMake(_scrollview.contentOffset.x + self.bounds.size.width, 0) animated:YES];
    }
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.page.currentPage = scrollView.contentOffset.x / self.bounds.size.width - 1;
    if (scrollView.contentOffset.x / self.bounds.size.width == 7) {
        self.page.currentPage = 0;
        scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    } else if (scrollView.contentOffset.x / self.bounds.size.width == 0) {
        self.page.currentPage = 5;
        scrollView.contentOffset = CGPointMake(6 * self.bounds.size.width, 0);
    }
}
- (void)pageAction {
    self.scrollview.contentOffset = CGPointMake((self.page.currentPage + 1) * self.bounds.size.width, 0);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
